program test
        type line
          character (len=2048) :: text		!! Input records are limited to 2048 characters
          integer :: lineno			!! Line number in the input file
          type (line), pointer :: next		!! Pointer to the next line
        end type line
        type (line), pointer     :: inputfile,L   !! Pointer to first line in the linked list of input file lines
	integer :: iomps=10

	open(iomps,file='contin.mge')
	call readinputfile

	L => inputfile
	do while (associated(L))
          write(*,'(1h ,a,i0,a,t12,a)') '[',L%lineno,']',trim(L%text)
	  L => L%next
	end do
	
contains

	subroutine readinputfile
	type (line), pointer :: L
        character (len=2048) :: input
	integer :: lineno
	integer :: ic

	nullify(L)
	lineno = 0
	do
          read(iomps,'(a)',end=100) input

	  if (input(1:1).eq.'+') then
	    if (.not.associated(L)) stop 'Cannot begin MGE file with a continuation line.'
	    if (len_trim(L%text)+len_trim(input).gt.len(L%text)) stop 'Input line is too long.'
	    L%text = trim(L%text)//' '//trim(input(2:))
	    lineno = lineno + 1
	    cycle
	  endif

	  lineno = lineno + 1

	  if (associated(L)) then
	    call newline(L%next)
	    L => L%next
	  else
	    call newline(inputfile)
	    L => inputfile
	  endif

	  L%text = input
	  L%lineno = lineno
	end do
100	return
	end subroutine readinputfile

	subroutine newline(L)
	type (line), pointer :: L
	nullify(L)
	allocate(L)
	nullify(L%next)
	L%lineno = 0
	L%text = ' '
	return
	end subroutine newline

end program test
